package com.mdh.runerrands.serviceImpl;

import com.mdh.runerrands.dao.CategoryDao;
import com.mdh.runerrands.dao.ProductDao;
import com.mdh.runerrands.pojo.Product;
import com.mdh.runerrands.pojo.ProductDetail;
import com.mdh.runerrands.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;

    @Resource
    private CategoryDao categoryDao;

    @Override
    public Product findById(int id) {
        return productDao.selectById(id);
    }

    @Override
    public int batchDelete(int ...ids) {
        return productDao.batchDelete(ids);
    }

    @Override
    public boolean deleteById(int id) {
        return productDao.deleteById(id);
    }

    @Override
    public List<Product> findByCategoryId(int categoryId) {
        return productDao.selectByCategoryId(categoryId);
    }

    @Override
    public List<Product> query(int page, int pageSize, String name, String description, String price,String status) {
        page = (page -1)*pageSize;
        return productDao.query(page,pageSize,name,description,price,status);
    }

    @Override
    public int selectCount(String name, String description, String price, String status) {
        return productDao.selectCount(name,description,price,status);
    }

    @Override
    public List<ProductDetail> queryDetail(int page, int pageSize, String name, String description, String price, String status) {
        List<ProductDetail> list = new ArrayList();
        page = (page -1)*pageSize;
        List<Product> query = productDao.query(page, pageSize, name, description, price, status);
        for (Product product :query){
            ProductDetail productDetail = new ProductDetail();
            productDetail.setId(product.getId());
            productDetail.setName(product.getName());
            productDetail.setDescription(product.getDescription());
            productDetail.setPrice(product.getPrice());
            productDetail.setStatus(product.getStatus());
            productDetail.setCategoryId(product.getCategoryId());
            productDetail.setCategory(categoryDao.selectById(product.getCategoryId()));
            list.add(productDetail);
        }
        return list;
    }

    @Override
    public boolean insert(Product product) {
        return productDao.insert(product);
    }

    @Override
    public boolean update(Product product) {
        return productDao.update(product);
    }
}
